package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorSetPassWorldRequest;
import org.tis.tools.abf.module.ac.entity.AcOperator;
import org.tis.tools.abf.module.ac.service.IAcOperatorService;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * Created by chenchao
 * Created on 2018/9/11 14:47
 */
@RestController
@Validated
@Api(description = "操作员对外服务")
@RequestMapping("/v1/api/acOperators")
@Slf4j
public class AcOperatorApiController {

    @Autowired
    private IAcOperatorService acOperatorService;


    @OperateLog(type = OperateType.UPDATE,desc = "修改操作员密码")
    @ApiOperation(value = "修改操作员密码", notes = "传入AcOperatorSetPassWorldRequest类作为参数")
    @PutMapping("/changePassword")
    public ResultVO changePassword(@RequestBody @Validated @ApiParam(name = "AcOperatorSetPassWorldRequest",value = "以AcOperatorSetPassWorldRequest类作为参数",required = true) AcOperatorSetPassWorldRequest passWorldRequest){

        if (!passWorldRequest.getNewFirstPassword().equals(passWorldRequest.getNewSecondPassword())){
            return ResultVO.error("404", "新密码和重复密码不同!");
        }
        AcOperator acOperatorQue  = acOperatorService.queryByUserId(passWorldRequest.getUserId());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除!");
        }
        if (StringUtil.isEmpty(passWorldRequest.getOldPassword())){
            return ResultVO.error("404", "旧密码不能为空!");
        }else{
            if (!passWorldRequest.getOldPassword().equals(acOperatorQue.getPassword())){
                return ResultVO.error("404", "旧密码与原密码不同!");
            }
            if (passWorldRequest.getOldPassword().equals(passWorldRequest.getNewFirstPassword())){
                return ResultVO.error("404", "旧密码与新密码相同!");
            }
        }

        acOperatorService.changePasswordApi(acOperatorQue,passWorldRequest);
        return ResultVO.success("密码修改成功!");
    }

    @OperateLog(type = OperateType.UPDATE,desc = "重置操作员密码")
    @ApiOperation(value = "重置操作员密码", notes = "传入AcOperatorSetPassWorldRequest类作为参数")
    @PutMapping("/resetPassword")
    public ResultVO resetPassword(@RequestBody @Validated @ApiParam(name = "AcOperatorSetPassWorldRequest",value = "以AcOperatorSetPassWorldRequest类作为参数",required = true) AcOperatorSetPassWorldRequest passWorldRequest){

        if (!passWorldRequest.getNewFirstPassword().equals(passWorldRequest.getNewSecondPassword())){
            return ResultVO.error("404", "新密码和重复密码不同!");
        }
        AcOperator acOperatorQue  = acOperatorService.queryByUserId(passWorldRequest.getUserId());
        if (acOperatorQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除!");
        }

        acOperatorService.changePasswordApi(acOperatorQue,passWorldRequest);
        return ResultVO.success("密码重置成功!");
    }

    @ApiOperation(value = "查询操作员的行为", notes = "传入登录用户名(userId)作为参数")
    @GetMapping("/{userId}/getBehaves")
    public ResultVO getBeahves(@PathVariable @NotBlank(message = "登录用户名不能为空") @ApiParam(name = "userId",value = "以userId作为参数",required = true) String userId){

        AcOperator acOperator = acOperatorService.queryByUserId(userId);
        if (null == acOperator){
            return ResultVO.error("404", "找不到对应记录或已经被删除!");
        }

        return ResultVO.success("查询成功!",acOperatorService.queryBehavesByOperator(acOperator));
    }

    @ApiOperation(value = "查询操作员的菜单,行为,个性化配置值", notes = "传入登录用户名(userId)作为参数")
    @GetMapping("/{userId}/getMenu")
    public ResultVO getMenu(@PathVariable @NotBlank(message = "登录用户名不能为空") @ApiParam(name = "userId",value = "以userId作为参数",required = true) String userId){

        AcOperator acOperator = acOperatorService.queryByUserId(userId);
        if (null == acOperator){
            return ResultVO.error("404", "找不到对应记录或已经被删除!");
        }

        return ResultVO.success("查询成功!",acOperatorService.queryMenuByOperator(acOperator));
    }

}

